/**
 * 
 */
package be.donovan.gui.helper.components;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Vector;



public abstract class DTOInterfaceController<T> extends AbstractController<DTOInterfacePanel> {

	public abstract Class<T> getDtoClass();

	public abstract Vector<String> getPropertyGetters();

	public abstract Vector<String> getPropertySetters();

	public abstract Vector<String> getHeading();

	protected abstract void saveAll();

	protected abstract Collection<T> readAll();

	protected List<T> toSaveOrUpdate = new ArrayList<T>();


	public void cleanup() {
		toSaveOrUpdate.clear();
	}

	public void onCloseAction(ActionEvent e) {
		stop();
	}

	@SuppressWarnings("unchecked")
	public void onNewAction(ActionEvent e) {
		T dto;
		try {
			dto = (T) getDtoClass().newInstance();
			((MVCTableModel) getView().catalogTable.getModel()).add(dto);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void preSaveAllAction(ActionEvent e) {
		toSaveOrUpdate.clear();
		getView().catalogTable.editingStopped(null);
		getView().buttonSave.requestFocus();
		for (int rowIndex = 0; rowIndex < getView().catalogTable.getRowCount(); rowIndex++) {
			toSaveOrUpdate.add((T) ((EditableMVCTableModel) getView().catalogTable.getModel())
					.fromRowVector(getView().catalogTable.convertRowIndexToModel(rowIndex)));
		}
//		validateAll();
	}

//	private void validateAll() {
//		ValidationException ve = new ValidationException();
//		if (setterException != null)
//			ve.addFaults(setterException.getFaults());
//		if (ve.containsFaults())
//			JOptionPane.showMessageDialog(getView(), ve.toString());
//		else
//			validationSucceeded = true;
//	}

	public void onSaveAllAction(ActionEvent e) {
		saveAll();
	}


	@Override
	public void initListeners() {
		setupActionListener(getView().buttonClose, "Close");

		setupActionListener(getView().buttonSave, "SaveAll");

		setupActionListener(getView().buttonNew, "New");
	}

	public void maintainCatalog() {
		getView().setName("Maintain " + getDtoClass().getSimpleName());
		getView().getController().start();
	}

	@SuppressWarnings("unchecked")
	@Override
	public void initData() {
		MVCTableModel tableModel = new EditableMVCTableModel(readAll(), getPropertyGetters(), getPropertySetters(), getHeading(),
				this);
		getView().catalogTable.setModel(tableModel);
		setTableCellEditors();
	}

	@Override
	public void doDeferredLayout() {
		super.doDeferredLayout();
		getView().catalogTable.packAll();
	}

	public void setTableCellEditors() {
	}

	/**
	 * Add a button to the buttonsPanel contained into the
	 * <code>DTOInterfacePanel</code>, and setup <code>ActionListener</code>
	 * to invoke on button click.<br>
	 * <b>Don't forget to override
	 * <code>onAdditionalButtonAction(ActionEvent e)</code> to do something
	 * with the button.</b>
	 * 
	 * @param text :
	 *            text to display on the button
	 * @param controller :
	 *            controller witch override the onAdditionalButtonAction method
	 *            called on this button click
	 */
//	public void addAdditionalButton(String text, DTOInterfaceController controller) {
//		JButton additionalButton = new JButton();
//		getView().buttonsPanel.add(additionalButton);
//		additionalButton.setText(text);
//		additionalController = controller;
//		setupActionListener(additionalButton, "AdditionalButton");
//	}

	/**
	 * @param e
	 */
//	@SuppressWarnings("unchecked")
//	public void onAdditionalButtonAction(ActionEvent e) {
//		if (additionalController != null) {
//			DTOInterfacePanel view = new DTOInterfacePanel();
//			view.setController(additionalController);
//			additionalController.maintainCatalog();
//		}
//	}


}
